package eu.urbain.gdocslauncher.exe;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.List;

import javax.swing.JOptionPane;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;

import eu.urbain.gdocslauncher.Common;
import eu.urbain.gdocslauncher.GDocsLauncherConfig;

public class Document {

	public static final String linkNew = "https://docs.google.com/document";
	private static List<String> allowedExtension = Arrays.asList("doc", "docx");

	// https://docs.google.com/document/d/1Pg053oU38AykmAV1X2k-oYi8KwCcqR7sk0emE3kUjH4/edit

	public static void main(String[] args) throws MalformedURLException {

		if (args != null && args.length != 0) {
			// path of the file top upload and open
			String path = args[0];

			// check if this file is openable with gdocs
			if (!Common.isValidPathAndFile(allowedExtension, path)) {
				JOptionPane.showMessageDialog(null, "Not a valid extension : " + path);
				System.exit(0);
			}

			// Initialization google stuff
			GoogleCredential credential;
			JsonFactory jsonFactory = new JacksonFactory();
			HttpTransport httpTransport = new NetHttpTransport();

			// try get previous authorization
			GDocsLauncherConfig gDocsLauncherConfig = Common.loadConfig();

			// check if application had authorization
			if (gDocsLauncherConfig == null) {
				// if not ask authorization
				gDocsLauncherConfig = Common.tryGetAuthorization();
				if (gDocsLauncherConfig == null) {
					// if refuse: cant upload file, exit
					System.exit(0);
				}
			}

			// upload file

			credential = new GoogleCredential.Builder().setClientSecrets(Common.CLIENT_ID, Common.CLIENT_SECRET).setJsonFactory(jsonFactory)
					.setTransport(httpTransport).build().setRefreshToken(gDocsLauncherConfig.getRefreshToken())
					.setAccessToken(gDocsLauncherConfig.getAccessToken());

			// Create a new authorized API client
			Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName("gDocsLauncher").build();

			// Insert a file
			File body = new File();
			body.setTitle("document.txt");
			//body.setDescription("A test document");
			body.setMimeType("text/plain");

			java.io.File currentDir = new java.io.File(".");
			String basePath;
			try {
				basePath = currentDir.getCanonicalPath();
				System.out.println(basePath);
				String documentTxtPath = basePath + "\\src\\resources\\document.txt";
				System.out.println(documentTxtPath);

				java.io.File fileContent = new java.io.File(documentTxtPath);
				FileContent mediaContent = new FileContent("text/plain", fileContent);

				File file = service.files().insert(body, mediaContent).execute();
				System.out.println("File ID: " + file.getId());

				// TODO get id of the file uploaded and open it
				Common.openWebpage(linkNew);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		else {
			// no path file --> open new doc
			Common.openWebpage(linkNew);
		}

	}

}
